<?php
namespace app\index\controller;
use app\index\model\Article as ArticleModel;
use think\Controller;
use think\exception\DbException;

/**
 * @SWG\Swagger(
 *   schemes={"http"},
 *   host="localhost",
 *   basePath="/thinkphp/public",
 *   consumes={"multipart/form-data","X-Requested-With/XMLHttpRequest"},
 *   produces={"application/json"},
 *   @SWG\Info(
 *     version="2.0.13",
 *     title="lblogApi",
 *     description="swagger2.0.13+swagger-ui2.2.10"
 *   ),
 *
 *   @SWG\Tag(
 *     name="Article",
 *     description="博文接口",
 *   ),
 * )
 */
class Article extends Controller
{

    /**
     * @SWG\Post(path="/article/add",
     *   tags={"Article"},
     *   summary="添加博文",
     *   description="",
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *     name="title",
     *     in="query",
     *     description="博文标题",
     *     required=true,
     *     type="string"
     *   ),@SWG\Parameter(
     *     name="content",
     *     in="query",
     *     description="博文内容，富文本内容",
     *     required=true,
     *     type="string"
     *   ),
     *   @SWG\Response(response=200, description="操作成功", @SWG\Schema(
     *         required={""},
     *         @SWG\Property(
     *              property="code",
     *              type="number",
     *              example=200,
     *         ),@SWG\Property(
     *              property="msg",
     *              type="string",
     *              example="博文[无标题:0]新增成功",
     *         ),@SWG\Property(
     *              property="data",
     *              type="array",
     *              @SWG\Items({})
     *         ),
     *     )),
     * )
     */
    // 新增博文
    public function add($title = '无标题',$content = '待补充')
    {
        $article = new ArticleModel;
        $article->article_title = $title;
        $article->article_content = $content;
        if ($article->save()) {
            jsonResponse(successCode,'博文[ ' . $article->article_title . ':' . $article->article_id . ' ]新增成功');
        } else {
            jsonResponse(errorCode,$article->getError());
        }
    }


    /**
     * @SWG\Get(path="/article/count",
     *   tags={"Article"},
     *   summary="获取博文总条数",
     *   description="",
     *   produces={"application/json"},
     *   @SWG\Response(response=200, description="操作成功", @SWG\Schema(
     *         required={""},
     *         @SWG\Property(
     *              property="code",
     *              type="number",
     *              example=200,
     *         ),@SWG\Property(
     *              property="msg",
     *              type="string",
     *              example="操作成功",
     *         ),@SWG\Property(
     *              property="data",
     *              type="number",
     *              example=0,
     *         ),
     *     )),
     * )
     */
    //获取博文总条数
    public function count()
    {
        try {
            $count = ArticleModel::all()->count();
            jsonResponse(successCode,successMsg,$count);
        } catch (DbException $e) {
            jsonResponse();
        }
    }


    /**
     * @SWG\Get(path="/article/read/{id}",
     *   tags={"Article"},
     *   summary="读取一条博文",
     *   description="",
     *   operationId="read",
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *     name="id",
     *     in="path",
     *     description="根据博文id获取博文信息",
     *     required=true,
     *     type="number"
     *   ),
     *   @SWG\Response(response=200, description="操作成功", @SWG\Schema(
     *         required={""},
     *           @SWG\Property(
     *              property="code",
     *              type="number",
     *              example=200,
     *         ),@SWG\Property(
     *              property="msg",
     *              type="string",
     *              example="操作成功",
     *         ),@SWG\Property(
     *              property="data",
     *              type="array",
     *              @SWG\Items(
     *     {
     *          @SWG\Property (property="article_id",type="number",description="博文id"),
     *          @SWG\Property (property="user_id",type="number",description="作者id"),
     *          @SWG\Property (property="article_title",type="string",description="博文标题"),
     *          @SWG\Property (property="article_content",type="string",description="博文内容"),
     *          @SWG\Property (property="article_views",type="number",description="浏览量"),
     *          @SWG\Property (property="article_comment_count",type="number",description="评论数"),
     *          @SWG\Property (property="article_date",type="datetime",description="发表时间"),
     *          @SWG\Property (property="article_like_count",type="number",description="点赞数"),
     *     }
     * )
     *         ),
     *     )),
     * )
     */
    // 读取一条博文
    public function read($id = 0)
    {
        try {
            $article = ArticleModel::get($id);
            jsonResponse(successCode,successMsg,$article);
            //echo json_encode($article,JSON_UNESCAPED_UNICODE);
            $this->viewsUp($article);
        } catch (DbException $e) {
            jsonResponse();
        }
    }


    /**
     * @SWG\Get(path="/article/readList",
     *   tags={"Article"},
     *   summary="读取一页博文",
     *   description="获取一页博文，参数分别为当前页，一页条数，默认第一页，一页十条",
     *   operationId="readList",
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *     name="curPage",
     *     in="query",
     *     description="当前页",
     *     required=true,
     *     type="number"
     *   ),@SWG\Parameter(
     *     name="pageSize",
     *     in="query",
     *     description="每页条数",
     *     required=true,
     *     type="number"
     *   ),
     *   @SWG\Response(response=200, description="操作成功", @SWG\Schema(
     *         required={""},
     *           @SWG\Property(
     *              property="code",
     *              type="number",
     *              example=200,
     *         ),@SWG\Property(
     *              property="msg",
     *              type="string",
     *              example="操作成功",
     *         ),@SWG\Property(
     *              property="data",
     *              type="string",
     *              example="{'per_page':'2','current_page':2,'has_more':true,'next_item':[{'article_id':26,'user_id':null,'article_title':'无标题','article_content':'待补充','article_views':3,'article_comment_count':0,'article_date':'2020-06-14 15:06:07','article_like_count':0}],'data':[{'article_id':28,'user_id':null,'article_title':'yayaya','article_content':'lalala','article_views':0,'article_comment_count':0,'article_date':'2020-06-14 18:49:16','article_like_count':0},{'article_id':27,'user_id':null,'article_title':'流年','article_content':'何为流年','article_views':0,'article_comment_count':0,'article_date':'2020-06-14 18:48:05','article_like_count':0}]}",
     *         ),
     *     )),
     * )
     */
    // 获取一页博文，参数分别为当前页，一页条数，默认第一页，一页十条
    public function readList($curPage = 1,$pageSize = 10)
    {
        try {
            $list = ArticleModel::order('article_date desc')
                ->paginate($pageSize,true,[
                'page'     => $curPage,
            ]);
            jsonResponse(successCode,successMsg,$list);
            //echo json_encode($list,JSON_UNESCAPED_UNICODE);
        } catch (DbException $e) {
            jsonResponse();
        }
    }


    /**
     * @SWG\Get(path="/article/timeList",
     *   tags={"Article"},
     *   summary="获取一截时间流",
     *   description="根据实时间获取一页博文，参数分别为当前页，一页条数，默认第一页，一页二十条",
     *   operationId="timeList",
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *     name="curPage",
     *     in="query",
     *     description="当前页",
     *     required=true,
     *     type="number"
     *   ),@SWG\Parameter(
     *     name="pageSize",
     *     in="query",
     *     description="每页条数",
     *     required=true,
     *     type="number"
     *   ),
     *   @SWG\Response(response=200, description="操作成功", @SWG\Schema(
     *         required={""},
     *           @SWG\Property(
     *              property="code",
     *              type="number",
     *              example=200,
     *         ),@SWG\Property(
     *              property="msg",
     *              type="string",
     *              example="操作成功",
     *         ),@SWG\Property(
     *              property="data",
     *              type="string",
     *              example="{'per_page':'2','current_page':2,'has_more':true,'next_item':[{'article_id':26,'article_title':'无标题','article_date':'2020-06-14 15:06:07'}],'data':[{'article_id':28,'article_title':'yayaya','article_date':'2020-06-14 18:49:16'},{'article_id':27,'article_title':'流年','article_date':'2020-06-14 18:48:05'}]}",
     *         ),
     *     )),
     * )
     */
    //获取一截时间流
    public function timeList($curPage = 1,$pageSize = 20)
    {
        try {
            $list = ArticleModel::field(['article_id','article_title','article_date'])
                ->order('article_date desc')
                ->paginate($pageSize,true,[
                'page'     => $curPage,
            ]);
            jsonResponse(successCode,successMsg,$list);
        } catch (DbException $e) {
            jsonResponse();
        }
    }


    /**
     * @SWG\Post(path="/article/update",
     *   tags={"Article"},
     *   summary="更新博文数据",
     *   description="",
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *     name="id",
     *     in="query",
     *     description="博文id",
     *     required=true,
     *     type="number"
     *   ),@SWG\Parameter(
     *     name="title",
     *     in="query",
     *     description="博文标题",
     *     required=true,
     *     type="string"
     *   ),@SWG\Parameter(
     *     name="content",
     *     in="query",
     *     description="博文内容，富文本内容",
     *     required=true,
     *     type="string"
     *   ),
     *   @SWG\Response(response=200, description="操作成功", @SWG\Schema(
     *         required={""},
     *         @SWG\Property(
     *              property="code",
     *              type="number",
     *              example=200,
     *         ),@SWG\Property(
     *              property="msg",
     *              type="string",
     *              example="操作成功",
     *         ),@SWG\Property(
     *              property="data",
     *              type="array",
     *              @SWG\Items({})
     *         ),
     *     )),
     * )
     */
    // 更新博文数据
    public function update($id = 0,$title = '',$content = '')
    {
        try {
            $article = ArticleModel::get($id);
            if ($id == null){
                jsonResponse(errorCode,'error:id不可为空');
            }
            if ($title != null){
                $article->article_title = $title;
            }
            if ($content != null){
                $article->article_content = $content;
            }
            if (false !== $article->save()) {
                jsonResponse(successCode,successMsg);
            } else {
                jsonResponse(errorCode,$article->getError());
            }
        } catch (DbException $e) {
            jsonResponse();
        }
    }


    /**
     * @SWG\Post(path="/article/delete",
     *   tags={"Article"},
     *   summary="删除博文数据",
     *   description="",
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *     name="id",
     *     in="query",
     *     description="博文id",
     *     required=true,
     *     type="number"
     *   ),
     *   @SWG\Response(response=200, description="操作成功", @SWG\Schema(
     *         required={""},
     *         @SWG\Property(
     *              property="code",
     *              type="number",
     *              example=200,
     *         ),@SWG\Property(
     *              property="msg",
     *              type="string",
     *              example="操作成功",
     *         ),@SWG\Property(
     *              property="data",
     *              type="array",
     *              @SWG\Items({})
     *         ),
     *     )),
     * )
     */
    // 删除博文数据
    public function delete($id)
    {
        try {
            $article = ArticleModel::get($id);
            if ($article) {
                $article->delete();
                jsonResponse(successCode,successMsg);
            } else {
                jsonResponse();
            }
        } catch (DbException $e) {
            jsonResponse();
        }
    }


    //浏览量加一
    private function viewsUp($article)
    {
        $article->article_views++;
        if (false !== $article->save()) {
            //jsonResponse(successCode,'id:'.$article['article_id'].'，浏览量加一');
        } else {
            //jsonResponse(errorCode,$article->getError());
        }
    }


    /**
     * @SWG\Post(path="/article/likeCountUp",
     *   tags={"Article"},
     *   summary="点赞数加一",
     *   description="",
     *   produces={"application/json"},
     *   @SWG\Parameter(
     *     name="id",
     *     in="query",
     *     description="博文id",
     *     required=true,
     *     type="number"
     *   ),
     *   @SWG\Response(response=200, description="操作成功", @SWG\Schema(
     *         required={""},
     *         @SWG\Property(
     *              property="code",
     *              type="number",
     *              example=200,
     *         ),@SWG\Property(
     *              property="msg",
     *              type="string",
     *              example="id:0，点赞数加一",
     *         ),@SWG\Property(
     *              property="data",
     *              type="array",
     *              @SWG\Items({})
     *         ),
     *     )),
     * )
     */
    //点赞数加一
    public function likeCountUp($id)
    {
        try {
            $article = ArticleModel::get($id);
            $article->article_like_count++;
            if (false !== $article->save()) {
                jsonResponse(successCode,'id:'.$article['article_id'].'，点赞数加一');
            } else {
                jsonResponse(errorCode,$article->getError());
            }
        } catch (DbException $e) {
            jsonResponse();
        }
    }

}